package creature;

import processing.core.PApplet;
import toxi.geom.Vec2D;

public class RectangleLayout extends Layout
{
	public RectangleLayout(int _numParticles)
	{
		super(_numParticles);
		type = TYPE_RECTANGLE;
	}
	
	public void generate(float _x, float _y, float _width, float _height)
	{
		generate(new Vec2D(_x, _y), _width, _height);
	}
	
	public void generate(Vec2D _pos, float _width, float _height)
	{
		pos = _pos;
		
		float numNodesPerSide = numNodes / 2;
		float nodeSpacingH = _width / numNodesPerSide;
		float nodeSpacingV = _height;
		float tx = pos.x - _width / 2 + nodeSpacingH / 2;
		float ty = pos.y - _height / 2;
		
		for (int i = 0; i < numNodes; i++)
		{
			particlePositions[i] = new Vec2D(tx, ty);
			
			if (i < (numNodes / 2) - 1)
			{
				tx += nodeSpacingH;
				springLengths[i] = nodeSpacingH;
			}
				
			if (i == (numNodes / 2) - 1)
			{
				ty += nodeSpacingV;
				springLengths[i] = nodeSpacingV;
			}
			
			if (i > (numNodes / 2) - 1)
			{
				tx -= nodeSpacingH;
				if (i == numNodes - 1)
				{
					springLengths[i] = nodeSpacingV;
				}
				else
				{
					springLengths[i] = nodeSpacingH;	
				}
			}
		}
		
		int writeIndex = numNodes;
		for (int i = 0; i < numNodes / 2; i++)
		{
			float d = particlePositions[i].sub(particlePositions[(numNodes-1)-i]).magnitude();
			springLengths[writeIndex] = d;
			writeIndex++;
		}
		
		float dax1 = particlePositions[0].sub(particlePositions[numNodes/2]).magnitude();
		springLengths[writeIndex] = dax1;
		writeIndex++;
		
		float dax2 = particlePositions[numNodes/2 - 1].sub(particlePositions[numNodes-1]).magnitude();
		springLengths[writeIndex] = dax2;
		writeIndex++;
		
		for (int i = 0; i < numNodes; i++)
		{
			float dist = pos.sub(particlePositions[i]).magnitude();
			springLengths[writeIndex] = dist;
			writeIndex++;
		}
		
//		for (int i = 0; i < springLengths.length; i++)
//		{
//			System.out.println(springLengths[i]);
//		}
	}
	
	private float getChordLength(float _radius, float _angle)
	{
		return 2.0f * _radius * (float) Math.sin(_angle * 0.5f);
	}
}
